﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Runtime.Serialization;

using SilverApp.ViewModel;

namespace SilverApp.View
{
    public partial class FileUploadWindow : ChildWindow
    {
        FolderViewModel parent;

        public FileUploadWindow()
        {
            InitializeComponent();
        }

        public FileUploadWindow(FolderViewModel dctx)
        {
            InitializeComponent();
            this.DataContext = dctx;
            parent = dctx;
        }

        private void bOpenFileDialog_Click(object sender, RoutedEventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = true;

            // Call the ShowDialog method to show the dialog box.
            bool? userClickedOK = openFileDialog1.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOK == true)
            {
                byte[] datas;
                using (Stream str = openFileDialog1.File.OpenRead())
                {
                    datas = new byte[str.Length];
                    str.Read(datas, 0, datas.Length);
                }

                LiveService.File f = new LiveService.File();
                f.name = openFileDialog1.File.Name;
                f.size = (int) openFileDialog1.File.Length;
                f.extension = openFileDialog1.File.Extension;
                parent.UploadFile(datas, f);
            
            }
        }
    }
}

